/*
 * @Description:
 * @version: 1.0.0
 * @Author: YinJianFeng
 * @Date: 2024-03-29 10:15:27
 */
/**
 * @author YinJianFeng
 * @since 2024-01-10
 */

import {
  ContextMenu,
  IContextMenu,
  MenuItem,
  STATE,
  useClipboard,
} from '@amcax/base';
import { Picker2D, PickerMgr } from '@amcax/renderer';
import { sketchEventCenter } from '@app-cad/environments/nextcad/events/SketchEventCenter';
import { useTools } from '@app-cad/common/hooks/useTools';
import { deleteSketchCruve } from '@app-cad/common/utils';

function Component(props: any) {
  const { copyObject2d } = useClipboard();
  const { picker2d } = useTools();

  function handleItemClick() {
    deleteSketchCruve();
  }

  const handleCopy = () => {
    const sketchId = sketchEventCenter().getSketchInfo().sketchId;
    const selectedModels = picker2d.getSelectedObjectInfo();

    copyObject2d(
      sketchId,
      selectedModels.map((v) => v.id),
    );
  };

  return (
    <ContextMenu id={props.id}>
      <MenuItem
        id='delete'
        onClick={handleItemClick}>
        删除
      </MenuItem>
      <MenuItem
        id='copy'
        onClick={handleCopy}>
        复制
      </MenuItem>
    </ContextMenu>
  );
}

const show = (): boolean => {
  if (sketchEventCenter().state === 'sleeping') {
    return false;
  }
  const selectedModels =
    PickerMgr.getPicker<Picker2D>('2d').getSelectedObjectInfo();
  if (sketchEventCenter().state !== STATE.DEFAULT) {
    return false;
  }
  return selectedModels.length > 0;
};

export const SketchContextMenu: IContextMenu = {
  id: 'sketchContextMenu',
  show,
  render: (id: string, params?: any) => (
    <Component
      id={id}
      key={`sketch-context-menu-${id}`}
      {...params}
    />
  ),
};
